// 通用伪造数据

const OWN_KEYS = ['label', 'value', 'title', 'id', 'text', 'key'];

const makeMockArray = (keyNum = 6, size = 11, keys = OWN_KEYS) => {
  const myKeys = [...keys];
  const divide = keyNum - keys.length;
  if (divide > 0) {
    for (let i = 0; i < divide; i += 1) {
      myKeys.push(`key${i + divide}`);
    }
  }
  return Array(size)
    .fill(1)
    .map((i, index) => {
      const obj = {};
      myKeys.forEach((k) => {
        obj[k] = index;
      });
      return obj;
    });
};

const makeMockObject = (keyNum = 1, keys = OWN_KEYS) => {
  const myKeys = [...keys];
  const divide = keyNum - keys.length;
  if (divide > 0) {
    for (let i = 0; i < divide; i += 1) {
      myKeys.push(`key${i + divide}`);
    }
  }
  const obj = {};
  myKeys.forEach((k, index) => {
    obj[k] = k + index;
  });
  return obj;
};

const t = (func, txt, v) => func.toString().replace(txt, JSON.stringify(v));

const getMockArrayHBS = (arrayLen = 11, isAsync = true) => {
  const value = makeMockArray(4, arrayLen);
  if (isAsync) {
    return t(async () => Promise.resolve(value), 'value', value);
  }
  return t(() => {}, 'value', value);
};

const getMockObjectHBS = (keyNum = 4, isAsync = true) => {
  if (isAsync) {
    return async () => Promise.resolve(makeMockObject(keyNum));
  }
  return () => makeMockObject(keyNum);
};

module.exports = {
  makeMockArray,
  makeMockObject,
  getMockArrayHBS,
  getMockObjectHBS,
};
